﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

namespace docTranslate
{
    public partial class UCTranslate : UserControl
    {
        private ArrayList fromLang = new ArrayList();
        private ArrayList toLang = new ArrayList();

        public UCTranslate()
        {
            InitializeComponent();
            LoadLanguages();
            Globals.ThisAddIn.Application.ActiveDocument.DetectLanguage();
            //if (Globals.ThisAddIn.Application.ActiveDocument.Content.LanguageDetected)
            //    ;
                //cbbFrom.SelectedValue = Globals.ThisAddIn.Application.ActiveDocument.Content.LanguageID;
                
        }
        /*
        Arabic - Saudia Arabia ...1025 
        Chinese - PRC ...2052 
        Chinese - Taiwan ...1028 
         * */

        private void LoadLanguages()
        {
            fromLang.Add(new AddValue("English USA", WdLanguageID.wdEnglishUS, "1033"));
            fromLang.Add(new AddValue("Portuguese Brazil", WdLanguageID.wdPortugueseBrazil, "1046"));
            fromLang.Add(new AddValue("Spanish", WdLanguageID.wdSpanish, "1034"));
            fromLang.Add(new AddValue("Japanese", WdLanguageID.wdJapanese, "1041"));
            fromLang.Add(new AddValue("French", WdLanguageID.wdFrench, "1036"));
            fromLang.Add(new AddValue("Italian", WdLanguageID.wdItalian, "1040"));
            fromLang.Add(new AddValue("German - Standard", WdLanguageID.wdGerman, "1031"));
            fromLang.Add(new AddValue("Dutch", WdLanguageID.wdDutch, "1043"));
            fromLang.Add(new AddValue("Korean", WdLanguageID.wdKorean, "1042"));
            fromLang.Add(new AddValue("Arabic - Saudia Arabia", WdLanguageID.wdArabic, "1025"));
            fromLang.Add(new AddValue("Chinese - PRC", WdLanguageID.wdChineseHongKongSAR, "2052"));
            fromLang.Add(new AddValue("Chinese - Taiwan", WdLanguageID.wdChineseSingapore, "1028"));
            
            toLang.Add(new AddValue("English USA", WdLanguageID.wdEnglishUS, "1033"));
            toLang.Add(new AddValue("Portuguese Brazil", WdLanguageID.wdPortugueseBrazil, "1046"));
            toLang.Add(new AddValue("Spanish", WdLanguageID.wdSpanish, "1034"));
            toLang.Add(new AddValue("Japanese", WdLanguageID.wdJapanese, "1041"));
            toLang.Add(new AddValue("French", WdLanguageID.wdFrench, "1036"));
            toLang.Add(new AddValue("Italian", WdLanguageID.wdItalian, "1040"));
            toLang.Add(new AddValue("German - Standard", WdLanguageID.wdGerman, "1031"));
            toLang.Add(new AddValue("Dutch", WdLanguageID.wdDutch, "1043"));
            toLang.Add(new AddValue("Korean", WdLanguageID.wdKorean, "1042"));
            toLang.Add(new AddValue("Arabic - Saudia Arabia", WdLanguageID.wdArabic, "1025"));
            toLang.Add(new AddValue("Chinese - PRC", WdLanguageID.wdChineseHongKongSAR, "2052"));
            toLang.Add(new AddValue("Chinese - Taiwan", WdLanguageID.wdChineseSingapore, "1028"));
            
            this.cbbFrom.DataSource = fromLang;
            this.cbbFrom.DisplayMember = "Display";
            this.cbbFrom.ValueMember = "LCID";
            this.cbbFrom.SelectedValue = -1;
            this.cbbTo.DataSource = toLang;
            this.cbbTo.DisplayMember = "Display";
            this.cbbTo.ValueMember = "LCID";
            this.cbbTo.SelectedValue = -1;
        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            object oMissing = System.Reflection.Missing.Value;
            Globals.ThisAddIn.Application.ActiveDocument.Save();
            Globals.ThisAddIn.CustomTaskPanes.Remove(Globals.ThisAddIn.TranslateCTP);
            Globals.Ribbons.Ribbon.btnTranslate.Checked = false;
            Paragraphs paragrafos = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs;
            int pCount = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs.Count;
            if (rdbInLine.Checked)
            {
                for (int i = pCount; i >= 1; i--)
                {
                    Paragraph p = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[i];
                    bool isTranslate;
                    string newText = LiveTranslatorWS.LiveTranslatorWS.Translate(p.Range.Text, cbbFrom.SelectedValue.ToString(), cbbTo.SelectedValue.ToString(), out isTranslate);
                    Paragraph newParagraph;
                    paragrafos[i].Range.InsertParagraphAfter();
                    newParagraph = paragrafos[i + 1];
                    newParagraph.Range.Bold = 1;
                    newParagraph.Range.Font.Color = WdColor.wdColorRed;
                    if (isTranslate)
                        newParagraph.Range.Text = newText;
                    else
                        newParagraph.Range.Text = p.Range.Text;
                }
            }
            else if (rdbEndDocument.Checked)
            {
                for (int i = pCount; i >= 1; i--)
                {
                    Paragraph p = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[i];
                    bool isTranslate;
                    string newText = LiveTranslatorWS.LiveTranslatorWS.Translate(p.Range.Text, cbbFrom.SelectedValue.ToString(), cbbTo.SelectedValue.ToString(), out isTranslate);
                    Paragraph newParagraph;
                    paragrafos[pCount].Range.InsertParagraphAfter();
                    newParagraph = paragrafos[pCount + 1];
                    newParagraph.Range.Bold = 1;
                    newParagraph.Range.Font.Color = WdColor.wdColorRed;
                    if (isTranslate)
                        newParagraph.Range.Text = newText;
                    else
                        newParagraph.Range.Text = p.Range.Text;
                }
            }
            else if (rdbSameDocument.Checked)
            {
                for (int i = pCount; i >= 1; i--)
                {
                    Paragraph p = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[i];
                    bool isTranslate;
                    string newText = LiveTranslatorWS.LiveTranslatorWS.Translate(p.Range.Text, cbbFrom.SelectedValue.ToString(), cbbTo.SelectedValue.ToString(), out isTranslate);
                    Paragraph newParagraph;
                    newParagraph = paragrafos[i];
                    if (isTranslate)
                        newParagraph.Range.Text = newText;
                    else
                        newParagraph.Range.Text = p.Range.Text;
                }
            }
            else if (rdbNewDocument.Checked)
            {
                //Start Word and create a new document.
                Word._Application oWord = new Word.Application();
                Word._Document oDoc;
                oWord.Visible = true;
                oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                oDoc.Save();

                //Insert a paragraph at the beginning of the document.
                Word.Paragraph firstParagraph;
                firstParagraph = oDoc.Content.Paragraphs.Add(ref oMissing);
                bool isTranslate;
                
                for (int i = pCount - 1; i >= 2; i--)
                {
                    firstParagraph.Range.InsertParagraphAfter();
                    Word.Paragraph newParagraph = oDoc.Content.Paragraphs[2];
                    Paragraph p = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[i];
                    string newText = LiveTranslatorWS.LiveTranslatorWS.Translate(p.Range.Text, cbbFrom.SelectedValue.ToString(), cbbTo.SelectedValue.ToString(), out isTranslate);
                    if (isTranslate)
                        newParagraph.Range.Text = newText;
                    else
                        newParagraph.Range.Text = p.Range.Text;
                }
                Paragraph firstP = Globals.ThisAddIn.Application.ActiveDocument.Paragraphs[1];
                string newFirstText = LiveTranslatorWS.LiveTranslatorWS.Translate(firstP.Range.Text, cbbFrom.SelectedValue.ToString(), cbbTo.SelectedValue.ToString(), out isTranslate);
                if (isTranslate)
                    firstParagraph.Range.Text = newFirstText;
                else
                    firstParagraph.Range.Text = firstP.Range.Text;
            }
        }

    }
}
